// Copyright 2012, Jun Chen .All rights reserved.
//
// Use of this source code is governed by a BSD-Style license that can be
// found in the License fule.
//
// Author: cpp.cheen at gmail dot com.


#ifndef EP_POLLER_H
#define EP_POLLER_H


#include    "pch_files.h"
#include    <vector>
#include    <map>
#include    <sys/epoll.h>

typedef     struct epoll_event      ep_event_t;

class   io_channel;

///
class ep_poller
{
    public:

    typedef std::vector<io_channel*>        io_channel_raw_ptr_vec;
    typedef std::map<x_fd,>        io_channel_raw_ptr_vec;
    public:
        /** Default constructor */
        ep_poller();
        /** Default destructor */
        virtual ~ep_poller();
        x_int32     get_event(io_channel_raw_ptr_vec& channels,x_int32 timeout_ms );


        /// can register channel,modify channel,remove channel
        x_int32 update_channel(io_channel*  channel);

        /// can register modify,remove io-event for a channel
        x_int32 update_subscribe_evt(io_channel*  channel);

        ///Since  Linux  2.6.8,  the max_fd  argument is unused.
        /// return epoll fd when success,or -1;
        static x_fd    create_ep_fd(x_int32 max_fd);
    protected:
        x_int32 poll_with_guard(io_channel_raw_ptr_vec& channels,x_int32 timeout_ms);
        x_int32 poll(io_channel_raw_ptr_vec& channels,x_int32 timeout_ms);

    private:
    enum    {max_event_for_poll_ = (x_uint16)2048};
    x_fd        m_epoll_fd;

};

#endif // EP_POLLER_H
